package com.rbcs.budget.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.MatchMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rbcs.RbcsConstants;
import com.rbcs.budget.model.BudgetItem;
import com.rbcs.budget.model.BudgetItemMonth;
import com.rbcs.flow.FlowConstants;
import com.systop.core.Constants;
import com.systop.core.service.BaseGenericsManager;

@Service
public class BudgetItemtManager extends BaseGenericsManager<BudgetItem> {

	/**
	 * 科目预算定制, 根据类别自动拆分为对应的[月度、季度]预算
	 * 
	 * @param bitem
	 */
	@Transactional
	public void splitSave(BudgetItem bitem, String type) {
		if (RbcsConstants.BUDGET_TYPE_QTR.equals(type)) {// 季度拆分
			splitSave(bitem, type, 1, 4);
		} else {// 月度拆分
			splitSave(bitem, type, 1, 12);
		}
	}

	@Transactional
	public void splitSave(BudgetItem bitem, String type, Integer start,
			Integer end) {
		save(bitem);
		double amount = bitem.getAmount();
		// 月预算额
		double monthAmount = 0;
		// 余数
		double odd = amount % end;
		if (odd == 0) {
			monthAmount = amount / end;
		} else {
			monthAmount = (amount - odd) / end;
		}
		for (int i = start; i <= end; i++) {
			BudgetItemMonth bim = new BudgetItemMonth();
			bim.setBudgetItem(bitem);
			if (RbcsConstants.BUDGET_TYPE_QTR.equals(type)) {
				bim.setQtr(i);
			} else {// 如果按照月度拆分，需要计算季度
				bim.setMonth(i);
				bim.setQtr(getQtr(i));
			}
			if (i != end) {
				bim.setAmount(monthAmount);
			} else {
				bim.setAmount(monthAmount + odd);
			}
			bim.setFinalAmount(bim.getAmount());
			getDao().save(bim);
		}
	}

	// 根据月份获得季度
	private Integer getQtr(Integer mon) {
		if (mon > 0 && mon < 4) {
			return 1;
		} else if (mon > 3 && mon < 7) {
			return 2;
		} else if (mon > 6 && mon < 10) {
			return 3;
		} else {
			return 4;
		}
	}

	/**
	 * 合计该科目月度预算
	 * 
	 * @param bi
	 */
	public void countItemBudget(BudgetItem bi) {
		Double amount = 0.0;
		for (BudgetItemMonth bim : bi.getBims()) {// 合计该科目的月度预算
			amount = amount + bim.getFinalAmount();
		}
		bi.setAmount(amount);
		save(bi);
	}

	/**
	 * 根据预算科目ID,部门ID,预算年度获得预算额[审核通过的]
	 * 
	 * @param itemId
	 * @param deptId
	 * @param year
	 * @return
	 */
	public BudgetItem getBudgetItem(Integer itemId, Integer deptId, Integer year) {
		String hql = "from BudgetItem bi where bi.item.id = ? and bi.budget.year = ? and bi.budget.dept.id = ? and bi.budget.audStatus = ? ";
		return findObject(hql, new Object[] { itemId, year, deptId, FlowConstants.AUDIT_PASS });
	}
	
	public Double sumItemByTopDept(Integer itemId, Integer year, String deptSerialNo){
		List<Object> args = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer();
		hql.append("select sum(bi.amount) from BudgetItem bi where bi.item.id = ? ");
		args.add(itemId);
		hql.append("and bi.budget.year = ? ");
		args.add(year);
		hql.append("and bi.budget.audStatus = ? ");//求和的条件必须审核通过
		args.add(FlowConstants.AUDIT_PASS);
		hql.append("and bi.budget.dept.isLeaf = ? ");//末端部门，好像没必要
		args.add(Constants.YES);
		hql.append("and bi.budget.dept.serialNo like ?");
		args.add(MatchMode.START.toMatchString(deptSerialNo));
		Double sumAmount = (Double)getDao().findObject(hql.toString(), args.toArray()); 
		if (sumAmount == null){
			sumAmount = 0.0;
		}
		return sumAmount;
	}

}
